import axios from 'axios'

// actionCreator
export const actionCreator = {
  init_table_list() {
    return async (dispatch) => {
      const res = await axios.get('/api/list')
      let data = res.data.data
      data.list.forEach((item) => {
        item.key = item.id
      })
      dispatch({
        type: 'init_table_list',
        payload: { list: data.list, total: data.total },
      })
    }
  },
}

const defaultState = {
  tableList: [],
  total: 0,
}

export default (state = defaultState, action) => {
  switch (action.type) {
    case 'init_table_list':
      return { ...state, tableList: action.payload.list, total: action.payload.total }

    default:
      return { ...state }
  }
}
